package atmGUI;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

//登录
public class LoginPanel extends JPanel implements ActionListener {
    private Icon loginImage = Util.getIcon("/image/buttonLand.jpg");//登陆按钮图
    private JButton loginBtn;
    private ATM_GUI_Main frame;//顶级容器实例
    private List<User> usersList;//用户信息集合实例
    private String account;//输入的账号
    private String password;//输入的密码
    private int errCount = 3;//输入错误次数
    private JTextField iptAccount;
    private JLabel accountJLabel;
    private JPasswordField iptPassword;
    private JLabel PasswordJLabel;
    private Font font = new Font("宋体", Font.PLAIN, 20);

    public LoginPanel(ATM_GUI_Main frame) {
        this.frame = frame;
//        this.usersList = usersList;//也可以传集合
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //登陆按钮
        loginBtn = new JButton(loginImage);//登录按钮
        loginBtn.setBounds(870, 220, 140, 50);
        this.add(loginBtn);
        loginBtn.addActionListener(this);//建立联系
        //输入账号提示插件
        accountJLabel = new JLabel("账号");
        accountJLabel.setBounds(280, 190, 100, 50);
        accountJLabel.setFont(font);
        this.add(accountJLabel);
        //输入账号框
        iptAccount = new JTextField();
        iptAccount.setBounds(380, 200, 300, 30);
        iptAccount.setFont(font);
        this.add(iptAccount);
        //输入密码提示插件
        PasswordJLabel = new JLabel("密码");
        PasswordJLabel.setBounds(280, 240, 100, 50);
        PasswordJLabel.setFont(font);
        this.add(PasswordJLabel);
        //输入密码框
        iptPassword = new JPasswordField();
        iptPassword.setBounds(380, 250, 300, 30);
        iptPassword.setFont(font);
        this.add(iptPassword);
    }

    @Override
    public void actionPerformed(ActionEvent e) {//当点击登录按钮时，切换到菜单界面
        if (e.getSource() == loginBtn) {
            account = iptAccount.getText();
            password = String.valueOf(iptPassword.getPassword());
            usersList = frame.getUsersList();
            for (int i = 0; i < frame.getUsersList().size(); i++) {
                if (usersList.get(i).getAccount().equals(account) && usersList.get(i).getPassword().equals(password)) {
                    //JOptionPane.showMessageDialog(this, "登陆成功");
                    frame.setLoginUser(usersList.get(i));
                    frame.switchPanel("menu");
                    errCount = 3;//成功后将输入错误机会还原
                    return;
                }
            }
            errCount--;
            if (errCount == 0) {
                JOptionPane.showMessageDialog(this, "输入账号密码机会次数已用完，请联系工作人员");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "输入账号密码机或密码错误，还剩" + errCount + "次机会");
        }

    }


    public JPasswordField getIptPassword() {
        return iptPassword;
    }

    public JTextField getIptAccount() {
        return iptAccount;
    }
}
